package com.chance.cc.crawler.development.bootstrap.lou;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.filter.FilterInfo;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.controller.DevCrawlerController;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRecordFilter.keyOrDateRange;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2021/2/1 17:15
 * @Description
 *     杭州19楼
 **/
public class Lou {
    private static final String domain = "19lou";

    public static void main(String[] args) {
        keywordRecord();
    }

    private static void keywordRecord(){
        String url = "https://www.19lou.com/search/thread/index";
        String site = "searchKw";

        FilterInfo count = new FilterInfo();
        count.setFilter(CrawlerEnum.CrawlerRecordFilter.count);
        count.setCurCount(1);
        count.setCountTotalNum(5);

        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPageItem)
                .domain(domain)
                .httpUrl(url)
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .turnPageFilterInfo(count)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24 * 30,null))
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .build();
        crawlerRequestRecord.setDownload(false);
        crawlerRequestRecord.setSkipPipeline(true);
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(domain);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(site);

        CrawlerRecord commentFilter = new CrawlerRequestRecord();
        commentFilter.setFilter(keyOrDateRange);
        commentFilter.addFilterInfo(FilterUtils.memoryFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,"queue")));
        commentFilter.addFilterInfo(FilterUtils.dateRangeFilterInfo(24 * 1,null));
        crawlerRequestRecord.tagsCreator().resultTags().getCategoryTag().addKVTag("comment_filter_record", JSON.toJSONString(commentFilter));
        crawlerRequestRecord.getHttpRequest().addExtra("searchKwSourceUrl","https://www.19lou.com/search/thread?keyword=%s&sorts=1&timeType=1&usesearchtype=1");
        crawlerRequestRecord.getHttpRequest().addExtra("Cookie","_Z3nY0d4C_=37XgPK9h; _DM_SID_=7e5cdd41db600d0fed239f4415825895; M_SMILEY_TIP_HIDE=1; _9755xjdesxxd_=32; gdxidpyhxdE=wriYS%5C1dGoft3m9vVzqrk1aM3LKYB%5CeNgNTAXtbov6ezPo3cfMgcoPOfuSEuqhy9svedgiNihqu8RNAgSo3js65jGQaj3at904uPyWkjB3h6I55gXauOR%5CJyTJP%2FP5BAfo7XGC%2B0ncbvxVpWv4MlogMfQYkPlxezXPJOv10qykAxmxiX%3A1612178212969; __snaker__captcha=7GfJFohW1iRwFYxg; JSESSIONID=7D26897B7299D7E1E9D5BCD6C8E9EE03; f9big=u63; _DM_S_=494a98d164e5d6d85f72ee14de093134; screen=1519; f39big=ip53; pm_count=%7B%7D; dayCount=%5B%5D; Hm_lvt_5185a335802fb72073721d2bb161cd94=1612170363,1614050723; fr_adv=; checkin__50631486_0223=1_4_6; sbs_auth_uid=50631486; sbs_auth_id=360ec74455c446c3b942a93e297f2c48_148fab5827e193a5c5dfa55a819658b6; sbs_auth_remember=1; _sbs_auth_uid=50631486; _sbs_auth_id=360ec74455c446c3b942a93e297f2c48_148fab5827e193a5c5dfa55a819658b6; dm_ui=50631486_20210223; loginwin_opened_user=50631486; fr_adv_last=search_post_filter_click_1; _dm_tagnames=%5B%7B%22k%22%3A%22nike%22%2C%22c%22%3A10%7D%2C%7B%22k%22%3A%22%E6%96%B0%E9%97%BB%E6%97%A9%E7%9F%A5%E9%81%93%22%2C%22c%22%3A4%7D%2C%7B%22k%22%3A%22%E5%81%A5%E5%BA%B7%E8%B5%84%E8%AE%AF%22%2C%22c%22%3A3%7D%2C%7B%22k%22%3A%22%E6%9D%AD%E5%B7%9E%22%2C%22c%22%3A1%7D%2C%7B%22k%22%3A%22%E4%B8%8A%E7%8F%AD%E6%97%8F%22%2C%22c%22%3A1%7D%2C%7B%22k%22%3A%22%E8%BD%A6%E5%8E%A2%22%2C%22c%22%3A1%7D%2C%7B%22k%22%3A%22%E5%AE%A2%E6%B5%81%22%2C%22c%22%3A1%7D%2C%7B%22k%22%3A%22%E7%AB%99%E7%82%B9%22%2C%22c%22%3A1%7D%2C%7B%22k%22%3A%22%E6%B5%99%E5%A4%A7%22%2C%22c%22%3A40%7D%2C%7B%22k%22%3A%22%E5%A4%96%E6%8B%8D%E6%B4%BB%E5%8A%A8%22%2C%22c%22%3A2%7D%5D; Hm_lpvt_5185a335802fb72073721d2bb161cd94=1614051847");

        CrawlerRequestRecord keywordRecord = CrawlerRequestRecord.builder()
                .startPageRequest("getArticle_keyword",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/v1/meta/"+domain+"/keys?site="+site)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        DevCrawlerController.builder()
                .triggerInfo(domain,domain,System.currentTimeMillis(),domain)
                .crawlerRequestQueue(DevCrawlerController.devRequestQueue(domain))
                .consoleResultPipeline()//控制台输出
//                .fileResultPipeline("D:\\chance\\log\\tets.log",true) //文件输出
                .requestRecord(crawlerRequestRecord)
                .supportRecord(keywordRecord)
                .build()
                .start();
    }
}
